package weaver.interfaces.zhlx.duss.action.purchasemodule.wms;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.WmsUtil;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 转料号流程调用WMS库存查询接口
 * @ModifyLog:
 **/
public class WeaZlhlcKcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        writeLog(">>>>>WeaZlhlcKcAction>>>>>requestid=" + requestid);
        try {
            String message = "调用WMS系统接口返回:";
            RecordSet rs = new RecordSet();
            rs.executeQuery("select id from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            rs.executeQuery("select id,lh,sl,kcgzh,ph,gc,kw,rownum from " + tablename + "_dt1 where mainid=? order by id", mainid);
            int failcount = 0;
            while (rs.next()) {
                JSONObject paramsJson = new JSONObject();
                String id = Util.null2String(rs.getString("id"));
                String ljbh = Util.null2String(rs.getString("lh"));
                double sl = Util.getDoubleValue(rs.getString("sl"), 0);
                String kcgzh = Util.null2String(rs.getString("kcgzh"));
                String ph = Util.null2String(rs.getString("ph"));
                String gc = Util.null2String(rs.getString("gc"));
                String kw = Util.null2String(rs.getString("kw"));
                String rownum = Util.null2String(rs.getString("rownum"));
                paramsJson.put("MATNR", ljbh);
                paramsJson.put("KZCODE", kcgzh);
                paramsJson.put("CHARG", ph);
                paramsJson.put("WERKS", gc);
                paramsJson.put("LGORT", kw);
                paramsJson.put("STATUS", "U");
                writeLog(">>>>>WeaZlhlcKcAction>>>>>paramsJson=" + paramsJson);
                JSONObject resultJson = WmsUtil.sendOKHttpRequestQuery(paramsJson);
                writeLog(">>>>>WeaZlhlcKcAction>>>>>resultJson=" + resultJson);
                String status = Util.null2String(resultJson.getString("code"));
                if ("success".equals(status)) {
                    String output = Util.null2String(resultJson.getString("OUTPUT"));
                    JSONObject outputJson = JSONObject.parseObject(output);
                    String msgType = outputJson.getString("MSG_TYPE");
                    if ("S".equalsIgnoreCase(msgType)) {
                        String dataReturnStr = outputJson.getString("DATA");
                        JSONArray dataList = JSONArray.parseArray(dataReturnStr);
                        if (!dataList.isEmpty()) {
                            for (int i = 0; i < dataList.size(); i++) {
                                JSONObject returnJson = dataList.getJSONObject(i);
                                double fxzkz = Util.getDoubleValue(returnJson.getString("AVAILABLEQTY"), 0);
                                rs.executeUpdate("update " + tablename + "_dt1 set kysl=? where id=?", fxzkz, id);
                                if (fxzkz < sl) {
                                    failcount++;
                                    message += rownum + "行库存数量不足;";
                                }
                            }
                        }else {
                            failcount++;
                            message += rownum + "行查询到结果为空;";
                        }
                    } else {
                        failcount++;
                        message += outputJson.getString("MSG_TEXT");
                    }
                } else {
                    failcount++;
                    message += resultJson.getString("message");
                }
            }
            if (failcount > 0) {
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaZlhlcKcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}